import _ from 'underscore'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'
import LineChart from './listLineChart.es6'
import PieChart from './listPieChart.es6'
import List from './listTable.es6'

export default class RootScope extends Scope {
  constructor(props) {
    super(props);

    this.dataSource = [
      new LineChart(props, this),
      new PieChart(props, this),
      new List(props, this)
    ];
  }

  state() {
    let newState = [{}];
    for (let i = 0; i < this.dataSource.length; i++) {
      newState.push(this.dataSource[i].$state);
    }
    return _.extend.apply(this, newState);
  }

  _emit(view) {
    if(!this.__unbind){
      view.setState(this.state())
    }
  }

  _handle(payload) {
    var payLoadName = '$' + payload.$type.toLowerCase()
    if(this[payLoadName]){
      this[payLoadName](payload)
    } else {
      for (let i = 0; i < this.dataSource.length; i++) {
        let item = this.dataSource[i];

        if (item[payLoadName]) {
          item[payLoadName].call(item, payload);
          break;
        }
      }
    }
  }

  $load() {
    for (let i = 0; i < this.dataSource.length; i++) {
      let item = this.dataSource[i];

      if (item.$load) {
        item.$load.call(item);
      }
    }
  }

  $settime(payload){
    this.$load();
  }
}
